20. Quiz: Practice Debugging

The following quiz questions focus on handling common errors or exceptions that you may encounter while executing your program.

QUIZ QUESTION::

The following are some common exceptions. Again, "exceptions" are errors detected during execution. See if you can recall and match each error with its meaning and the steps you will take to handle the exception. If you need help, feel free to do some research online to learn more about the exception.

ANSWER CHOICES:



Example Exception

How would you try to handle the exception?

You are trying to access a local variable before it is defined. Make sure local scope of variable in function is defined or value assigned to it.

An operation or function is applied to an object of inappropriate type - e.g., trying to concatenate a string and integer. Change the datatype for one of the values (e.g change int to str)

Identifier is not found in the local or global namespace. Make sure the reference to the identifier is correctly added to the code.

Assignation error. Inconsistency in how many values being unpacked and how many variables the values should be assigned to.

SOLUTION:

Example Exception

How would you try to handle the exception?

You are trying to access a local variable before it is defined. Make sure local scope of variable in function is defined or value assigned to it.

An operation or function is applied to an object of inappropriate type - e.g., trying to concatenate a string and integer. Change the datatype for one of the values (e.g change int to str)

Identifier is not found in the local or global namespace. Make sure the reference to the identifier is correctly added to the code.

Assignation error. Inconsistency in how many values being unpacked and how many variables the values should be assigned to.

Quiz: Practice Debugging

In the workspace at the bottom of the page, there is a piece of code in the ** user_input_numlist.py** Python file. The code prompts the user to enter 10 two-digit numbers. It should then find and print the sum of all of the even numbers among those that were entered.

But there is a bug in the code! When I input a number, I get the following TypeError. Use the programming environment provided below with a Terminal and code editor to debug the code.

** Using the workspace:**

Here are a few tips orienting you to this kind of workspace.

In the top panel is a code editor where you can edit the Python file ** user_input_numlist.py**. Scroll up and down in this panel to see all the code. You can also expand or shrink this panel by clicking and dragging its bottom border.

In the bottom panel, you can execute your Python file by clicking on New Terminal and entering ** python user_input_numlist.py** on the command line.

The solutions have been provided on the next page, but I encourage you to try figuring out where the bug is in the code, and fixing it yourself.

** Sample Output:**
This is what the output should look like.

>>> user_list: [23, 24, 25, 26, 27, 28, 29, 30, 31, 22]
>>> The sum of the even numbers in user_list is: 130.

Workspace

This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity, so you may be able to download them there.

Workspace Information:

  • Default file path:
  • Workspace type: generic
  • Opened files (when workspace is loaded): n/a
  • userCode:

    python user_input_numlist.py